﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InformaticaDiaryWeb.Models;
using InformaticaDiaryWeb.Service;
using InformaticaDiary.Common;

namespace InformaticaDiaryWeb.Controllers
{
    public class ResultController : Controller
    {

        [HttpGet]
        public ActionResult ResultCaseNo(string id)
        {
            DataAccess objDataAccess = new DataAccess();
            SearchCaseNoModel objSearchCriteria = (SearchCaseNoModel)Session["searchreq"];

            ResultCaseNoModel objResultCaseNoModel = new ResultCaseNoModel();
            objResultCaseNoModel.CaseTypeID = objSearchCriteria.CaseTypeID;
            objResultCaseNoModel.CaseYear = objSearchCriteria.CaseYear;
            objResultCaseNoModel.strCaseNo = objSearchCriteria.strCaseNo;
            objResultCaseNoModel.SearchType = "CaseNo";

            objResultCaseNoModel.listCaseType = objDataAccess.GetCaseType().Select(i => new SelectListItem()
            {
                Text = i.CaseTypeName.ToString(),
                Value = i.CaseTypeID.ToString()
            });
            objResultCaseNoModel.listCaseYear = objDataAccess.GetYear().Select(i => new SelectListItem()
            {
                Text = i.ToString(),
                Value = i.ToString()
            });

            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    objResultCaseNoModel.objSearchResult = objdb.DoSearch(new SearchValue { SearchType = objResultCaseNoModel.SearchType, UserID = 2, CaseTypeID = objSearchCriteria.CaseTypeID, CaseYear = objSearchCriteria.CaseYear, TextField = objSearchCriteria.strCaseNo });
                    if (objResultCaseNoModel.objSearchResult == null || objResultCaseNoModel.objSearchResult.Count() < 1)
                    {
                        objResultCaseNoModel.isRecordExist = "false";
                    }
                    else
                        objResultCaseNoModel.isRecordExist = "true";
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }
            //This is a landing page for redirects, so set the URL Hash manually for JQM mobile
            ViewData["JQMOptions"] = new JQMOptions() { Url = "~/Result/ResultCaseNo" };


            return View(objResultCaseNoModel);

        }

        [HttpPost]
        public ActionResult ResultCaseNo(ResultCaseNoModel objFilterResults)
        {

            DataAccess objDataAccess = new DataAccess();
            objFilterResults.listCaseType = objDataAccess.GetCaseType().Select(i => new SelectListItem()
            {
                Text = i.CaseTypeName.ToString(),
                Value = i.CaseTypeID.ToString()
            });
            objFilterResults.listCaseYear = objDataAccess.GetYear().Select(i => new SelectListItem()
            {
                Text = i.ToString(),
                Value = i.ToString()
            });
            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    var obj = objdb.DoSearch(new SearchValue { SearchType = objFilterResults.SearchType, UserID = 2, CaseTypeID = objFilterResults.CaseTypeID, CaseYear = objFilterResults.CaseYear, TextField = objFilterResults.strCaseNo });
                    objFilterResults.objSearchResult = obj;
                    if (objFilterResults.objSearchResult == null || objFilterResults.objSearchResult.Count() < 1)
                    {
                        objFilterResults.isRecordExist = "false";
                    }
                    else
                        objFilterResults.isRecordExist = "true";

                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return View(objFilterResults);
        }

        [HttpGet]
        public ActionResult ResultPetitioner(string id)
        {
            DataAccess objDataAccess = new DataAccess();
            SearchPetitionerModel objSearchCriteria = (SearchPetitionerModel)Session["searchreq"];

            ResultPetitionerModel objResultCaseNoModel = new ResultPetitionerModel();
            objResultCaseNoModel.CaseYear = objSearchCriteria.CaseYear;
            objResultCaseNoModel.PetitionerName = objSearchCriteria.PetitionerName;
            objResultCaseNoModel.SearchType = "Petitioner";

            objResultCaseNoModel.listCaseYear = objDataAccess.GetYear().Select(i => new SelectListItem()
            {
                Text = i.ToString(),
                Value = i.ToString()
            });

            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    objResultCaseNoModel.objSearchResult = objdb.DoSearch(new SearchValue { SearchType = objResultCaseNoModel.SearchType, UserID = 2, CaseTypeID = 0, CaseYear = objSearchCriteria.CaseYear, TextField = objSearchCriteria.PetitionerName });
                    if (objResultCaseNoModel.objSearchResult == null || objResultCaseNoModel.objSearchResult.Count() < 1)
                    {
                        objResultCaseNoModel.isRecordExist = "false";
                    }
                    else
                        objResultCaseNoModel.isRecordExist = "true";
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return View(objResultCaseNoModel);

        }

        [HttpPost]
        public ActionResult ResultPetitioner(ResultPetitionerModel objFilterResults)
        {

            DataAccess objDataAccess = new DataAccess();
            objFilterResults.listCaseYear = objDataAccess.GetYear().Select(i => new SelectListItem()
            {
                Text = i.ToString(),
                Value = i.ToString()
            });


            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    objFilterResults.objSearchResult = objdb.DoSearch(new SearchValue { SearchType = objFilterResults.SearchType, UserID = 2, CaseTypeID = 0, CaseYear = objFilterResults.CaseYear, TextField = objFilterResults.PetitionerName });
                    if (objFilterResults.objSearchResult == null || objFilterResults.objSearchResult.Count() < 1)
                    {
                        objFilterResults.isRecordExist = "false";
                    }
                    else
                        objFilterResults.isRecordExist = "true";
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return View(objFilterResults);
        }

        [HttpGet]
        public ActionResult ResultRespondant(string id)
        {
            DataAccess objDataAccess = new DataAccess();
            SearchRespondantModel objSearchCriteria = (SearchRespondantModel)Session["searchreq"];

            ResultRespondantModel objResultCaseNoModel = new ResultRespondantModel();          
            objResultCaseNoModel.CaseYear = objSearchCriteria.CaseYear;
            objResultCaseNoModel.RespondantName = objSearchCriteria.RespondantName;
            objResultCaseNoModel.SearchType = "Respondant";
           
            objResultCaseNoModel.listCaseYear = objDataAccess.GetYear().Select(i => new SelectListItem()
            {
                Text = i.ToString(),
                Value = i.ToString()
            });

            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    objResultCaseNoModel.objSearchResult = objdb.DoSearch(new SearchValue { SearchType = objResultCaseNoModel.SearchType, UserID = 2, CaseTypeID = 0, CaseYear = objSearchCriteria.CaseYear, TextField = objSearchCriteria.RespondantName });
                    if (objResultCaseNoModel.objSearchResult == null || objResultCaseNoModel.objSearchResult.Count() < 1)
                    {
                        objResultCaseNoModel.isRecordExist = "false";
                    }
                    else
                        objResultCaseNoModel.isRecordExist = "true";
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return View(objResultCaseNoModel);

        }

        [HttpPost]
        public ActionResult ResultRespondant(ResultRespondantModel objFilterResults)
        {

            DataAccess objDataAccess = new DataAccess();

            objFilterResults.listCaseYear = objDataAccess.GetYear().Select(i => new SelectListItem()
            {
                Text = i.ToString(),
                Value = i.ToString()
            });


            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    var obj = objdb.DoSearch(new SearchValue { SearchType = objFilterResults.SearchType, UserID = 2, CaseTypeID = 0, CaseYear = objFilterResults.CaseYear, TextField = objFilterResults.RespondantName });
                    objFilterResults.objSearchResult = obj;
                    if (objFilterResults.objSearchResult == null || objFilterResults.objSearchResult.Count() < 1)
                    {
                        objFilterResults.isRecordExist = "false";
                    }
                    else
                        objFilterResults.isRecordExist = "true";
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return View(objFilterResults);
        }

        [HttpGet]
        public ActionResult ResultJudgement(string judg)
        { 
            ResultJudgementModel objResultCaseNoModel = new ResultJudgementModel();
            objResultCaseNoModel.OrderJudgement = judg;
            objResultCaseNoModel.SearchType = "Judgement";

            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    objResultCaseNoModel.objSearchResult = objdb.DoSearch(new SearchValue { SearchType = objResultCaseNoModel.SearchType, UserID = 2, CaseTypeID = 0, CaseYear = 0, TextField = judg });
                    if (objResultCaseNoModel.objSearchResult == null || objResultCaseNoModel.objSearchResult.Count() < 1)
                    {
                        objResultCaseNoModel.isRecordExist = "false";
                    }
                    else
                        objResultCaseNoModel.isRecordExist = "true";
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return View(objResultCaseNoModel);

        }

        [HttpPost]
        public ActionResult ResultJudgement(ResultJudgementModel objFilterResults)
        {           
            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    var obj = objdb.DoSearch(new SearchValue { SearchType = objFilterResults.SearchType, UserID = 2, CaseTypeID = 0, CaseYear = 0, TextField = objFilterResults.OrderJudgement });
                    objFilterResults.objSearchResult = obj;
                    if (objFilterResults.objSearchResult == null || objFilterResults.objSearchResult.Count() < 1)
                    {
                        objFilterResults.isRecordExist = "false";
                    }
                    else
                        objFilterResults.isRecordExist = "true";
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return View(objFilterResults);
        }
     

        [HttpGet]
        public ActionResult CaseItem(string id = "0")
        {
            CaseDetailsModel objCaseDetailsModel = new CaseDetailsModel();
            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess objdb = new DataAccess();
                    CaseDetails objCaseDetails = objdb.GetCaseDetails(Convert.ToInt64(id));
                    objCaseDetailsModel = MapCaseDetails(objCaseDetails);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }
            return View(objCaseDetailsModel);

        }
        private CaseDetailsModel MapCaseDetails(CaseDetails obj)
        {
            CaseDetailsModel obj1 = new CaseDetailsModel
            {
                CaseDetailsID = obj.CaseDetailsID,
                UserID = obj.UserID,
                UserName = obj.UserName,
                CourtID = obj.CourtID,
                CourtName = obj.CourtName,
                CourtType = obj.CourtType,
                CaseTypeId = obj.CaseTypeId,
                CaseTypeName = obj.CaseTypeName,
                CaseYear = obj.CaseYear,
                PoliceStationID = obj.PoliceStationID,
                StageID = obj.StageID,
                PoliceStation = obj.PoliceStation,
                Stage = obj.Stage,
                Fileno = obj.Fileno,
                CaseNo = obj.CaseNo,
                BackDate = obj.BackDate.ToShortDateString(),
                InstitutionDate = obj.InstitutionDate.ToShortDateString(),
                NextDate = obj.NextDate.ToShortDateString(),
                Petitioner = obj.Petitioner,
                Respondant = obj.Respondant,
                PetitionerMob = obj.PetitionerMob,
                RespondantMob = obj.RespondantMob,
                PetitionerDetails = obj.PetitionerDetails,
                RespondantDetails = obj.RespondantDetails,
                PetitionerAdvocate = obj.PetitionerAdvocate,
                RespondantAdvocate = obj.RespondantAdvocate,
                FirNo = obj.FirNo,
                FirYear = obj.FirYear,
                OrderJudgement = obj.OrderJudgement
            };
            return obj1;
        }
    }
}
